package com.letoken.platform.repo.app.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.admin.req.user.CapPledgeMiningRecordPageReq;
import com.letoken.platform.repo.admin.req.user.FundInfoRq;
import com.letoken.platform.repo.app.po.AppFundDetail;
import com.letoken.platform.repo.app.po.AppFundInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.letoken.platform.repo.app.po.AppFundLevelInfo;
import com.letoken.platform.repo.app.po.AppFundV2Detail;
import com.letoken.platform.repo.app.req.user.QueueWeightingReq;
import com.letoken.platform.repo.app.req.user.UserLevelReq;
import com.letoken.platform.repo.app.res.assets.MingListRes;
import com.letoken.platform.repo.app.res.assets.QueryTokenAssetsHistoryRes;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 作者
 * @since 2023-08-04
 */
public interface AppFundInfoMapper extends BaseMapper<AppFundInfo> {


    @Select("select 1 from  app_fund_info where user_id = #{userId} limit 1")
    Integer existsFund(@Param("userId") Integer userId);


    @Select("select user_id  from app_fund_info where expiration_time>NOW() and type =1  GROUP BY user_id")
    List<Integer> getFundUserId();

    @Select("select user_id  from app_fund_info where expiration_time>NOW() and create_time<='2023-09-20 00:00:00' GROUP BY user_id")
    List<Integer> getupdateFundUserId();

    @Select("select * from app_fund_info where expiration_time>NOW() and type =1 and user_id = #{userId} ")
    List<AppFundInfo> getFundByUserId(@Param("userId")Integer userId);

    @Select("select * from app_fund_info where expiration_time>NOW() and type =1 and user_id = #{userId} and create_time<'2023-10-14 00:00:00'")
    List<AppFundInfo> getFundByUserIdUpdate(@Param("userId")Integer userId);

    @Select("select * from app_fund_info where expiration_time>NOW() and create_time<='2023-09-20 00:00:00' and user_id = #{userId}")
    List<AppFundInfo> getupdateFundByUserId(@Param("userId")Integer userId);

    @Select(" select max(fund_rate) from  admin_fund_level_config limit 1")
    BigDecimal getMaxfundRate();

    List<UserLevelReq>  selectUserLevel(@Param("userId")Integer userId);
    @Select("select COUNT(`level`) count,`level`  from app_mall_ambassador where `level`>0 GROUP BY `level` HAVING COUNT(`level`) >0")
    List<QueueWeightingReq> getCountByLevel();

    @Select("select user_id userId,level  from app_fund_level_info where level >= #{level}")
    List<QueueWeightingReq> getUserByLevel(@Param("level")Integer level);

    @Select("select user_id from app_mall_ambassador where `level` = #{level}}")
    List<Integer> getUserIdByLevel(@Param("level")Integer level);
    @Select("select IFNULL(sum(fund),0) from app_fund_info where user_id=#{userId} and fund_id=#{fundId} and type =1 and expiration_time > NOW()")
    BigDecimal getFundAmountByType(@Param("userId")Integer userId,@Param("fundId")Integer fundId);

    Page<AppFundInfo> getFundInfoList(@Param("userId")Integer userId, @Param("page") Page<AppFundInfo> page,@Param("type")Integer type);

    Page<AppFundV2Detail> getFundDetailoList(@Param("userId")Integer userId,@Param("page")Page<AppFundV2Detail> page);

    Page<AppFundDetail> getFundDetailCAPList(@Param("userId")Integer userId,@Param("page")Page<AppFundDetail> page);
   // @Select("select retained from app_fund_v2_info where user_id=#{userId}")
    @Select("select ifnull(retained,0) from app_fund_v2_info where user_id=#{userId}")
    BigDecimal sumAmount(@Param("userId")Integer userId,@Param("type")Integer type);

    @Select("select IFNULL(sum(fund),0) from app_fund_info where expiration_time > NOW() and type =1  and  create_time >= '2023-10-12 15:00:00'")
    BigDecimal adminSumAmount();
    @Select("select IFNULL(sum(fund),0) from app_fund_info where  create_time >= '2023-10-12 15:00:00'")
    BigDecimal adminSumAmountHistory();

    @Select("select IFNULL(sum(fund),0) from app_fund_info where expiration_time > NOW() and type =1 and fund_id =#{fundId} and  create_time >= '2023-10-12 15:00:00'")
    BigDecimal adminSumAmountByType(@Param("fundId")Integer fundId);
    @Select("select IFNULL(sum(fund),0) from app_fund_info where fund_id =#{fundId} and  create_time >= '2023-10-12 15:00:00' ")
    BigDecimal adminSumAmountHistoryByType(@Param("fundId")Integer fundId);


   Page<AppFundInfo> fundInfoDetail(@Param("page")Page<AppFundInfo> page,@Param("req") FundInfoRq req);

    List<UserLevelReq>  selectUserLevelPeerRate(@Param("userId")Integer userId);

   BigDecimal  getMaxLevelRate();
   @Select("select user_id from app_fund_info GROUP BY user_id")
   List<Integer> getFundUser();


   @Select("select * from app_fund_info where expiration_time<NOW() and type =1 and state=1")
   List<AppFundInfo> getExpireList();


    @Select("select user_id userId,SUM(fund) fund from app_fund_info where type=1 and expiration_time> NOW() and state =1 GROUP BY user_id")
   List<AppFundInfo> getSumFundByUserId();

    @Update("UPDATE `app_fund_info` SET  `type` = 2 WHERE user_id = #{userId}")
    void updateTypeByUserId(@Param("userId")Integer userId);

    @Select("select * from app_fund_info where create_time>'2023-10-12 14:00:00' and create_time<'2023-10-13 00:00:00' and fund_id>=3")
    List<AppFundInfo> updateDateList();

    @Select("select * from app_fund_level_info  where `level`=5")
    List<AppFundLevelInfo> getLevelInfoList();

    @Select("select * from app_fund_level_info  where `level`=5 and user_id not in (16541289,16546394,16548127,16549419)")
    List<AppFundLevelInfo> updateLevelInfoList();


    @Select("SELECT COUNT(1) FROM app_fund_level_info i WHERE i.user_id IN (SELECT user_id FROM app_user_team t WHERE t.inviter_id = #{userId} ) AND i.`level` >= 5 AND i.update_time >= '2024-1-29 00:00:00'")
    Integer getLevelInfoListByInviterId(@Param("userId")Integer userId);

    @Select("select ifnull(SUM(fund),0) from app_fund_info where fund_id=3 and user_id=#{userId} and type=1 and expiration_time>NOW()")
    BigDecimal sumFundByUserIdAndType(@Param("userId")Integer userId);

    Integer getLevel(@Param("userId")Integer userId);

    @Select("select a.id,a.title,a.date,a.day_rate rate,a.url,a.background_url backgroundUrl,a.min, IFNULL((select SUM(fund) from app_fund_info where fund_id=a.id and type=1 and expiration_time>NOW() and user_id=#{userId}),0) sumAmout from admin_fund_config a ORDER BY a.id asc")
    List<MingListRes> getMingListRes(@Param("userId") Integer userId);


    @Select({"<script>",
            " select ifnull(SUM(fund),0)" +
                    " from app_fund_info" +
                    "<where>" +
                    "   <choose>" +
                    "       <when test='null != fundId'> " +
                    "           fund_id = #{fundId}" +
                    "       </when>" +
                    "       <otherwise> " +
                    "           fund_id in (1,2,3)" +
                    "       </otherwise>" +

                    "   </choose>" +
                    "   <if test='null != type'> " +
                    "      and type = #{type}" +
                    "   </if>" +
                    "   <if test='null != currentTime'> " +
                    "      and expiration_time &gt;= #{currentTime}" +
                    "   </if>" +
                    "</where>" +
                    "</script>"})
    BigDecimal sum(@Param("type") Integer type, @Param("fundId") Integer fundId, @Param("currentTime") LocalDateTime currentTime);

    @Select({"<script>",
            " select afi.user_id, afi.fund_id, afi.fund, afi.create_time, afi.expiration_time , afi.type, afc.title " +
            " from app_fund_info afi inner join admin_fund_config afc on afi.fund_id = afc.id" +
            "<where>" +
            "   <if test='null != req.fundId'> " +
            "           fund_id = #{req.fundId}" +
            "   </if>" +
            "   <if test='null != req.userId'> " +
            "      and afi.user_id = #{req.userId}" +
            "   </if>" +
            "   <choose>" +
            "       <when test='3 == req.type'> " +
            "          and afi.type = 1 and afi.expiration_time &lt; now() " +
            "       </when>" +
            "       <when test='1 == req.type'> " +
            "          and afi.type = 1 and afi.expiration_time &gt;= now() " +
            "       </when>" +
            "       <when test='2 == req.type'> " +
            "          and afi.type = 2 " +
            "       </when>" +
            "   </choose>" +
            "   <if test='null != req.startTime'> " +
            "      and afi.create_time &gt;= #{req.startTime}" +
            "   </if>" +
            "   <if test='null != req.endTime'> " +
            "     and  afi.create_time &lt;= #{req.endTime}" +
            "   </if>" +
            "   <if test='null != req.dueStartTime'> " +
            "      and afi.expiration_time &gt;= #{req.dueStartTime}" +
            "   </if>" +
            "   <if test='null != req.dueEndTime'> " +
            "     and  afi.expiration_time &lt;= #{req.dueEndTime}" +
            "   </if>" +
            "</where>" +
            "</script>"})
    Page<AppFundInfo> page(@Param("page") Page page, @Param("req") CapPledgeMiningRecordPageReq req);


    @Select("select sum(fund) from app_fund_info where expiration_time>NOW() and type =1  and user_id = #{userid}")
    BigDecimal selectSumInterestAccruingByUserId(Integer userid);
}
